package com.peng.redis;

import com.peng.utils.JacksonUtil;
import java.util.List;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.args.ListPosition;

/**
 * JedisWithListSample
 *
 * @author lupeng
 * Created on 2022-04-19
 */
public class JedisWithListSample {

    private static final Logger logger = LoggerFactory.getLogger(JedisWithListSample.class);

    private JedisPool pool;

    public static final String KEY = "listkey";
    public static final String KEY1 = "listkey1";

    @BeforeEach
    public void init() {
        pool = new JedisPool("192.168.64.1", 6379);
        logger.info("JedisPool create success");
    }

    @AfterEach
    public void destroy() {
        if (pool != null) {
            pool.close();
        }
    }


    @Test
    public void lpushTest() {
        try (Jedis jedis = pool.getResource()) {
            for (int i = 0; i < 100; i++) {
                long lpush = jedis.lpush(KEY, String.valueOf(i));
                logger.info("lpush {}", lpush);
            }
        }
    }


    @Test
    public void lpushxTest() {
        try (Jedis jedis = pool.getResource()) {
            long lpushx = jedis.lpushx(KEY, "1");
            logger.info("lpushx {}", lpushx);
        }
    }

    @Test
    public void rpushTest() {
        try (Jedis jedis = pool.getResource()) {
            long rpush = jedis.rpush(KEY, "9999");
            logger.info("rpush {}", rpush);
        }
    }

    @Test
    public void lpopAndRpopTest() {
        try (Jedis jedis = pool.getResource()) {
            for (int i = 0; i < 10; i++) {
                String lpop = jedis.lpop(KEY);
                logger.info("lpop {}", lpop);

                String rpop = jedis.rpop(KEY);
                logger.info("rpop {}", rpop);
            }
        }
    }


    @Test
    public void lpoprpushTest() {
        try (Jedis jedis = pool.getResource()) {
            for (int i = 0; i < 10; i++) {
                jedis.lpush(KEY, String.valueOf(i));
            }

            for (int i = 0; i < 99; i++) {
                jedis.rpoplpush(KEY, KEY);
            }

            List<String> all = jedis.lrange(KEY, 0, -1);
            logger.info("all {}", JacksonUtil.toJson(all));
        }
    }

    @Test
    public void lremllenTest() {
        try (Jedis jedis = pool.getResource()) {
            List<String> all = jedis.lrange(KEY, 0, -1);
            logger.info("all {}", JacksonUtil.toJson(all));

            // count > 0从开头向结尾移除元素 count > 0 从结尾向前移除
            long lrem = jedis.lrem(KEY, 10, "8");
            logger.info("lrem {}", lrem);

            logger.info("all {}", JacksonUtil.toJson(jedis.lrange(KEY, 0, -1)));

            logger.info("llen {}", jedis.llen(KEY));

            logger.info("lindex {}", jedis.lindex(KEY, 1));

            // 向指定元素后面插入元素
            logger.info("lindex {}", jedis.linsert(KEY, ListPosition.AFTER, "7", "111"));
            logger.info("all {}", JacksonUtil.toJson(jedis.lrange(KEY, 0, -1)));
        }
    }

    // 覆盖指定元素的内容
    @Test
    public void lset() {
        try (Jedis jedis = pool.getResource()) {
            jedis.lset(KEY, 3, "2");
            logger.info("all {}", JacksonUtil.toJson(jedis.lrange(KEY, 0, -1)));
        }
    }

    @Test
    public void ltrim() {
        try (Jedis jedis = pool.getResource()) {
            jedis.ltrim(KEY, 10, 30);
            logger.info("all {}", JacksonUtil.toJson(jedis.lrange(KEY, 0, -1)));
        }
    }

}
